package hai.com.android_test.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import hai.com.android_test.R;

/**
 * Created by 黄海 on 2016/11/22.
 */

public class VpFragment extends Fragment {

    List<String> list = new ArrayList<>();
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;


    public static VpFragment newInstance(String title) {
        VpFragment vpFragment = new VpFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        vpFragment.setArguments(bundle);
        return vpFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.vpfragment, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        String title = (String) getArguments().get("title");
        for (int i = 0; i < 100; i++) {
            list.add(new String("title" + i));
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(new RecyclerView.Adapter<MyHold>() {

            @Override
            public MyHold onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false);
                return new MyHold(view);
            }

            @Override
            public void onBindViewHolder(MyHold holder, int position) {
                holder.tv.setText(list.get(position).toString());
            }

            @Override
            public int getItemCount() {
                return list.size();
            }


        });
    }


    class MyHold extends RecyclerView.ViewHolder {
        TextView tv;

        public MyHold(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv);
        }
    }

}
